/*

  The MIT License

  Copyright (c) 2008 - 2010. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/
package
{

import com.bit101.components.Label;
import com.bit101.components.PushButton;
import com.bit101.components.TextArea;
import flash.display.Bitmap;
import ru.goodcore.core.GoodCore;

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.system.ApplicationDomain;
import flash.system.System;
import flash.utils.ByteArray;
import flash.utils.getTimer;

import ru.goodcore.composite.Branch;
import ru.goodcore.composite.CompositeEvent;
import ru.goodcore.composite.Cradle;
import ru.goodcore.composite.Leaf;
import ru.goodcore.utils.ActivityListener;
import ru.goodcore.utils.traceByteArray;

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="400", height="300", frameRate="21", backgroundColor="#E0EEEE")]

public class CompositeTreeTest extends GoodCore {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function CompositeTreeTest() {
		super();
		super.percentWidth = 100;
		super.percentHeight= 100;
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 */
	private var _dataBase:			Cradle;

	/**
	 * @private 
	 */
	private var _sampleList:		Array = new Array();
	
	private var _textField:			TextArea;

	/**
	 * @private 
	 */
	private var _exit:				Boolean = true;
	
	private var _numObjects:		uint = 5e4;

	private var _nestedLevel:		uint = 10;

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	protected override function creationComplete():void {
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		var activityListener:ActivityListener = new ActivityListener();
		activityListener.width = 150;
		activityListener.x = 5;
		activityListener.y = 5;
		super.stage.addChild(activityListener);

		var button:PushButton = new PushButton(this, 10, 70, 'play', this.handler_pressPlay);

		this._textField = new TextArea(this, 10, 100);
		this._textField.width = 200;
		this._textField.height = 180;
		this._textField.text = 'numObjects: ' + this._numObjects + ', nestedLevel: ' + this._nestedLevel + '\n';
		this._dataBase = new Cradle();
		this._dataBase.addEventListener(CompositeEvent.ADDED, this.handler_added);
		this._dataBase.addEventListener(CompositeEvent.ADDED_TO_CRADLE, this.handler_addedToCradle);
		//this.startTest();
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function startTest():void {
		var numObjects:		uint = this._numObjects;
		var nestedLevel:	uint = this._nestedLevel;
		var loop:			uint = numObjects / nestedLevel;
		var parent:			Branch;
		var dc:				Branch;

		this._dataBase.removeAllChildren();

		System.gc();

		var startTime:		int = getTimer();

		for (var i:uint = 0; i < loop; i++) {
			parent = this._dataBase;

			for (var j:uint = 0; j < nestedLevel; j++) {
				//parent = parent.addChildAt(new Branch(), 0) as Branch;
				parent = parent.addChild(new Branch()) as Branch;
				//parent.dispatchEvent(new QueryEvent() );
			}

		}

		var duration:int = getTimer() - startTime;
		this.calculateAverage(duration);
	}


	/**
	 * @private
	 */
	private function calculateAverage(duration:int):void {
		this._sampleList.push(duration);

		var average:	int;

		this._sampleList.forEach(aaa);
		
		
		function aaa(element:*, index:int, arr:Array):void {
			average += int(element);
		}
		
		var memory:Number = System.totalMemory / 1024;
		trace('duration', duration, 'average', int(average / this._sampleList.length), 'memory', memory);
		this._textField.text += 'average: ' + int(average / this._sampleList.length) + ', memory: ' + memory + '\n';

	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	private function handler_added(event:Event):void {
		event;
	}

	private function handler_addedToCradle(event:Event):void {
		event;
	}

	private function handler_queryResult(event:Event):void {
		event;
	}

	/**
	 * @private
	 */	
	private function handler_pressPlay(event:Event):void {
		this.startTest();

		//return;
		this._exit = !this._exit;

		if (this._exit) {
			super.removeEventListener(Event.ENTER_FRAME, this.handler_enterFrame);
		} else {
			super.addEventListener(Event.ENTER_FRAME, this.handler_enterFrame);
		}

	}

	/**
	 * @private
	 */	
	private function handler_enterFrame(event:Event):void {
		if ( !this._exit )
			this.startTest();
	}


}
}